/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package introse;

import com.sun.net.ssl.internal.ssl.Debug;

public class sourceViewer extends javax.swing.JPanel {

    /**
     * Creates new form NewJPanel
     */
    private sourceManager sourceMngr;
    private uneditableTableModel sourceTableModel;

    public sourceViewer() {
        initComponents();
    }

    public void loadSourceTable() {
        sourceMngr = new sourceManager();
        sourceTableModel = new uneditableTableModel(new String[]{"Source Number", "Is Active", "Source Name"});
        sourceTable.setModel(sourceTableModel);

        sourceTable.removeColumn(sourceTable.getColumnModel().getColumn(0));
        sourceTable.removeColumn(sourceTable.getColumnModel().getColumn(0));

        for (source src : sourceMngr.getSources(true)) {
            sourceTableModel.addRow(new String[]{Integer.toString(src.getSourceNumber()), Boolean.toString(src.getIsActive()), src.getSourceName()});
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane5 = new javax.swing.JScrollPane();
        sourceTable = new javax.swing.JTable();
        addSourceButton = new javax.swing.JButton();
        editSourceButton = new javax.swing.JButton();
        deleteSourceButton = new javax.swing.JButton();
        reactivateSources = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(1000, 600));

        sourceTable.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        sourceTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Source"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane5.setViewportView(sourceTable);

        addSourceButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Inventory Add.png"))); // NOI18N
        addSourceButton.setText("<html> <p align = \"center\"> Add  Source </p> </html>");
        addSourceButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        addSourceButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        addSourceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addSourceButtonActionPerformed(evt);
            }
        });

        editSourceButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Inventory Edit.png"))); // NOI18N
        editSourceButton.setText("<html> <p align = \"center\"> Edit Source </p> </html>");
        editSourceButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        editSourceButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        editSourceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editSourceButtonActionPerformed(evt);
            }
        });

        deleteSourceButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Inventory Minus.png"))); // NOI18N
        deleteSourceButton.setText("Deactivate Source");
        deleteSourceButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        deleteSourceButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        deleteSourceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteSourceButtonActionPerformed(evt);
            }
        });

        reactivateSources.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Revert Source.png"))); // NOI18N
        reactivateSources.setText("Reactivate Sources");
        reactivateSources.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        reactivateSources.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        reactivateSources.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reactivateSourcesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 849, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(reactivateSources, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(deleteSourceButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(editSourceButton)
                    .addComponent(addSourceButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addSourceButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editSourceButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteSourceButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(reactivateSources)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addSourceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addSourceButtonActionPerformed
        new addSource(null, true).setVisible(true);
        loadSourceTable();
    }//GEN-LAST:event_addSourceButtonActionPerformed

    private void editSourceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editSourceButtonActionPerformed
        try {
            int sourceNumber = Integer.parseInt(sourceTableModel.getValueAt(sourceTable.getSelectedRow(), 0).toString());
            if (sourceNumber == 1) {
                errorDialogue errorMessage = new errorDialogue(null, true);
                errorMessage.setMessage("Main Office cannot be edited");
                errorMessage.setVisible(true);
            } else if (sourceNumber > 0) {
                editSource editSrc = new editSource(null, true);
                editSrc.setSource(sourceMngr.getSource(Integer.parseInt(sourceTableModel.getValueAt(sourceTable.getSelectedRow(), 0).toString())));
                editSrc.setVisible(true);
            }
        } catch (Exception ex) {
            errorDialogue errorMessage = new errorDialogue(null, true);
            errorMessage.setMessage("No source selected");
            errorMessage.setVisible(true);
        }
        loadSourceTable();
    }//GEN-LAST:event_editSourceButtonActionPerformed

    private void deleteSourceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteSourceButtonActionPerformed
        try {
            int sourceNumber = Integer.parseInt(sourceTableModel.getValueAt(sourceTable.getSelectedRow(), 0).toString());

            if (sourceNumber == 1) {
                errorDialogue errorMessage = new errorDialogue(null, true);
                errorMessage.setMessage("Main Office cannot be deactivated");
                errorMessage.setVisible(true);
            } else if (sourceNumber > 0) {
                deactivationDialog delDiag = new deactivationDialog(null, true);
                delDiag.setVisible(true);
                if (delDiag.getReturnStatus() == 1) {
                    sourceMngr.getSource(sourceNumber).setIsActive(false);
                    positiveMessage posMsg = new positiveMessage(null, true);
                    posMsg.setMessage("Source has been deactivated");
                    posMsg.setVisible(true);
                }
            }
        } catch (Exception ex) {
            errorDialogue errorMessage = new errorDialogue(null, true);
            errorMessage.setMessage("No source selected");
            errorMessage.setVisible(true);
        }
        loadSourceTable();
    }//GEN-LAST:event_deleteSourceButtonActionPerformed

    private void reactivateSourcesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reactivateSourcesActionPerformed
        reactivateSources reactSrcs = new reactivateSources(null, true);
        reactSrcs.setVisible(true);
        loadSourceTable();
    }//GEN-LAST:event_reactivateSourcesActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addSourceButton;
    private javax.swing.JButton deleteSourceButton;
    private javax.swing.JButton editSourceButton;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JButton reactivateSources;
    private javax.swing.JTable sourceTable;
    // End of variables declaration//GEN-END:variables
}
